package top.kaoshanji.ch04;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 代码清单 ch04-01：用Java连接到服务器(4.1.2节)
 * 执行是没有结果的
 * author kaoshanji
 * ^_^
 * create time 2024/12/7 18:22
 */
public class SocketTest {

    private static final Logger logger = LogManager.getLogger(SocketTest.class);

    static final String URL_HOST = "time.nist.gov";

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单 ch04-01：用Java连接到服务器(4.1.2节)...");

        try (var s = new Socket(URL_HOST, 12);
             var in = new Scanner(s.getInputStream(), StandardCharsets.UTF_8)) {
            while (in.hasNextLine()) {
                String line = in.nextLine();
                System.out.println(line);
            }
        }

    }

}
